<?php


namespace Tbf\Bundle\AlipayBundle;


use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Router;
use Tbf\Component\Path\PathFunction;

/**
 * 支付宝全局配置
 * 卖家信息优先级：seller_id>seller_account_name>seller_email。
 */
class Config {
    /**
     * 卖家id
     * @var string
     */
    protected $seller_id;
    /**
     * 卖家帐号名
     * @var string
     */
    protected $seller_account_name;
    /**
     * 卖家email
     * @var string
     */
    protected $seller_email;

    /**
     * 支付宝 合作者身份ID
     * @var string
     */
    protected $app_key;
    /**
     * 交易安全校验码
     * @var string
     */
    protected $app_secret;
    /**
     * 网站起始url,形如:http://xxx.com/
     * 配置这个东西
     * 1.防止host伪造攻击
     * 2.在某些情况下(命令行),没有请求域名信息
     * @var string
     */
    protected $web_url_base;
    /**
     * @var string
     */
    protected $alipay_gateway='https://mapi.alipay.com/gateway.do';
    /**
     * 即时到帐页面回调地址
     * @var string
     */
    protected $cdbu_return_url;
    /**
     * 即时到帐api回调地址
     * @var string
     */
    protected $cdbu_notify_url;
    /**
     * 标准双接口页面回调地址
     * @var string
     */
    protected $ctbu_return_url;
    /**
     * 标准双接口api回调地址
     * @var string
     */
    protected $ctbu_notify_url;
    /**
     * 纯担保交易页面回调地址
     * @var string
     */
    protected $cptbb_return_url;
    /**
     * 纯担保交易页面回调地址
     * @var string
     */
    protected $cptbb_notify_url;
    /**
     * @var RouteCollection
     */
    protected $router;
    public function __construct(
                                $app_key,
                                $app_sercet,
                                $web_url_base,
                                Router $router
        ){
        $this->app_key = $app_key;
        $this->app_secret = $app_sercet;
        $this->web_url_base = $web_url_base;
        $this->router = $router;
    }

    /**
     * @return string
     */
    public function getAppKey()
    {
        return $this->app_key;
    }

    /**
     * @return string
     */
    public function getAppSecret()
    {
        return $this->app_secret;
    }

    /**
     * @return string
     */
    public function getSellerEmail()
    {
        return $this->seller_email;
    }

    /**
     * @param string $cdbu_notify_url
     */
    public function setCdbuNotifyUrl($cdbu_notify_url)
    {
        $this->cdbu_notify_url = $cdbu_notify_url;
    }

    /**
     * @return string
     */
    public function getCdbuNotifyUrl()
    {
        if (is_string($this->cdbu_notify_url)){
            return $this->cdbu_notify_url;
        }
        return $this->cdbu_notify_url = PathFunction::urlJoin($this->web_url_base,
            $this->router->generate('tbf_alipay_callback_create_direct_by_user_api'));
    }

    /**
     * @param string $cdbu_return_url
     */
    public function setCdbuReturnUrl($cdbu_return_url)
    {
        $this->cdbu_return_url = $cdbu_return_url;
    }

    /**
     * @return string
     */
    public function getCdbuReturnUrl()
    {
        if (is_string($this->cdbu_return_url)){
            return $this->cdbu_return_url;
        }
        return $this->cdbu_return_url = PathFunction::urlJoin($this->web_url_base,
            $this->router->generate('tbf_alipay_callback_create_direct_by_user_page'));
    }

    /**
     * @param string $ctbu_notify_url
     */
    public function setCtbuNotifyUrl($ctbu_notify_url)
    {
        $this->ctbu_notify_url = $ctbu_notify_url;
    }

    /**
     * @return string
     */
    public function getCtbuNotifyUrl()
    {
        if (is_string($this->ctbu_notify_url)){
            return $this->ctbu_notify_url;
        }
        return $this->ctbu_notify_url = PathFunction::urlJoin($this->web_url_base,
            $this->router->generate('tbf_alipay_callback_create_trade_by_user_api'));
    }

    /**
     * @param string $ctbu_return_url
     */
    public function setCtbuReturnUrl($ctbu_return_url)
    {
        $this->ctbu_return_url = $ctbu_return_url;
    }

    /**
     * @return string
     */
    public function getCtbuReturnUrl()
    {
        if (is_string($this->ctbu_return_url)){
            return $this->ctbu_return_url;
        }
        return $this->ctbu_return_url = PathFunction::urlJoin($this->web_url_base,
            $this->router->generate('tbf_alipay_callback_create_trade_by_user_page'));
    }

    /**
     * @return string
     */
    public function getCptbbNotifyUrl()
    {
        return $this->ctbu_return_url = PathFunction::urlJoin($this->web_url_base,
            $this->router->generate('tbf_alipay_callback_create_partner_trade_by_buyer_api'));
    }

    /**
     * @return string
     */
    public function getCptbbReturnUrl()
    {
        return $this->ctbu_return_url = PathFunction::urlJoin($this->web_url_base,
            $this->router->generate('tbf_alipay_callback_create_partner_trade_by_buyer_page'));
    }

    /**
     * @param string $alipay_gateway
     */
    public function setAlipayGateway($alipay_gateway)
    {
        $this->alipay_gateway = $alipay_gateway;
    }

    /**
     * @return string
     */
    public function getAlipayGateway()
    {
        return $this->alipay_gateway;
    }

    /**
     * @param string $seller_account_name
     */
    public function setSellerAccountName($seller_account_name)
    {
        $this->seller_account_name = $seller_account_name;
    }

    /**
     * @return string
     */
    public function getSellerAccountName()
    {
        return $this->seller_account_name;
    }

    /**
     * @param string $seller_id
     */
    public function setSellerId($seller_id)
    {
        $this->seller_id = $seller_id;
    }

    /**
     * 卖家id
     * @return string
     */
    public function getSellerId()
    {
        return $this->seller_id;
    }

    /**
     * 返回卖家帐号信息,请求用数组
     * 实践表明app_key的值等于seller_id
     */
    public function getSellerAccountRequestArray(){
        if (!empty($this->seller_id)){
            return array('seller_id'=>$this->seller_id);
        }
        if (!empty($this->seller_account_name)){
            return array('seller_account_name'=>$this->seller_account_name);
        }
        if (!empty($this->seller_email)){
            return array('seller_email'=>$this->seller_email);
        }
        return array('seller_id'=>$this->app_key);
    }

    /**
     * @param string $seller_email
     */
    public function setSellerEmail($seller_email)
    {
        $this->seller_email = $seller_email;
    }

    /**
     * @param string $app_key
     */
    public function setAppKey($app_key)
    {
        $this->app_key = $app_key;
    }

    /**
     * @param string $app_secret
     */
    public function setAppSecret($app_secret)
    {
        $this->app_secret = $app_secret;
    }

}